﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Linq;
using JsalesModel;


namespace JSales
{
    /// <summary>
    /// 全局静态变量
    /// </summary>
    public class GlobalVar
    {
        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <returns>返回连接字符串</returns>
        public static string GetConn()
        {
            MySec sec = new MySec();
            string conn = "Data Source="
                          + ConfigurationManager.AppSettings["dbServer"].ToString()
                          + ","
                          + ConfigurationManager.AppSettings["dbPort"].ToString()
                          + ";Initial Catalog="
                          + ConfigurationManager.AppSettings["dbName"].ToString()
                          + ";Persist Security Info=True;User ID="
                          + ConfigurationManager.AppSettings["dbUser"].ToString()
                          + ";Password="
                          //+ ";Password=JOrder";
                          + sec.Decrypto(ConfigurationManager.AppSettings["dbPswd"].ToString());
            return conn;
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public static JsalesModel.TbUser cUser;

        public static IQueryable<TbDept> GDepts;
        public static IQueryable<TbUser> GUsers;
        public static IQueryable<TbRole> GRoles ;
        public static IQueryable<TbRight> GRights ;        
        public static IQueryable<TbCustomer> GCustomers ;
        public static IQueryable<TbProduct> GProductsRaw; //原纸
        public static IQueryable<TbProduct> GProductsSemi; //半成品
        public static IQueryable<TbProduct> GProducts; //成品纸
        public static IQueryable<TbProduct> GProductsAll; //全部纸类
        public static IQueryable<TbPtKind> GKinds ;
        public static IQueryable<TbPrice> GPrices ;
        public static IQueryable<TbColor> GColors ;
        /// <summary>
        /// 克重
        /// </summary>
        public static IQueryable<TbGrammage> GGrammages ; 
        /// <summary>
        /// 程度
        /// </summary>
        public static IQueryable<TbDegree> GDegrees ;
        /// <summary>
        /// 纹路
        /// </summary>
        public static IQueryable<TbTexture> GTextures;
        /// <summary>
        /// 工艺
        /// </summary>
        public static IQueryable<TbProcess> GProcesses;
        public static IQueryable<TbLevel> GLevels;
        public static IQueryable<TbStore> GStores;
        public static IQueryable<TbCusLevel> GCusLevels;

        public static System.ComponentModel.BindingList<ProductTypes>  typeses=new BindingList<ProductTypes>();

        public static void InitTypes()
        {
            typeses.Add(new ProductTypes(0,"原纸"));
            typeses.Add(new ProductTypes(1, "半成品"));
            typeses.Add(new ProductTypes(2, "成品"));
        }

        public static void SelectDept()
        {
            var dbConText = new JYEntities();
            if(dbConText.TbDepts.Any())
                GDepts = dbConText.TbDepts;
        }

        public static void SelectUsers()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbUsers.Any())
                GUsers = dbConText.TbUsers;
        }

        public static void SelectRole()
        {
            var dbConText = new JYEntities();
            if(dbConText.TbRoles.Any())
                GRoles = dbConText.TbRoles;
        }

        public static void SelectRight()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbRights.Any())
                GRights = dbConText.TbRights;
        }

        public static void SelectCustomer()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbCustomers.Any())
                GCustomers = dbConText.TbCustomers;
        }

        //原纸
        public static void SelectProductRaw()
        {
            var dbConText = new JYEntities();
            IQueryable<TbProduct> p =from i in dbConText.TbProducts
                                where i.Types==0
                                select i;
            if (p.Any())
                GProductsRaw = p;
        }

        //半成品
        public static void SelectProductSemi()
        {
            var dbConText = new JYEntities();
            IQueryable<TbProduct> p = from i in dbConText.TbProducts
                                      where i.Types == 1
                                      select i;
            if (p.Any())
                GProductsSemi = p;
        }

        //成品纸
        public static void SelectProduct()
        {
            var dbConText = new JYEntities();
            IQueryable<TbProduct> p = from i in dbConText.TbProducts
                                      where i.Types == 2
                                      select i;
            if (p.Any())
                GProducts = p;
        }

        public static void SelectProductAll()
        {
            IQueryable<TbProduct> p = from i in GlobalVar.GProducts.Union(GlobalVar.GProductsSemi).Union(GlobalVar.GProductsRaw)
                                        where i.IsDeleted == false
                                        select i;
            if (p.Any())
            {
                GProductsAll = p;
            }
        }

        public static void SelectKind()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbPtKinds.Any())
                GKinds = dbConText.TbPtKinds;
        }

        public static void SelectPrice()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbPrices.Any())
                GPrices = dbConText.TbPrices;
        }
        
        public static void SelectColor()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbColors.Any())
                GColors = dbConText.TbColors;
        }

        public static void SelectGrammage()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbGrammages.Any())
                GGrammages = dbConText.TbGrammages;
        }

        public static void SelectDegree()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbDegrees.Any())
                GDegrees = dbConText.TbDegrees;
        }

        public static void SelectTexture()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbTextures.Any())
                GTextures = dbConText.TbTextures;

        }

        public static void SelectProcess()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbProcesses.Any())
                GProcesses = dbConText.TbProcesses;

        }

        public static void SelectLevel()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbLevels.Any())
                GLevels = dbConText.TbLevels;
        }

        public static void SelectStore()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbStores.Any())
                GStores = dbConText.TbStores;
        }

        public static void SelectCusLevel()
        {
            var dbConText = new JYEntities();
            if (dbConText.TbCusLevels.Any())
                GCusLevels = dbConText.TbCusLevels;
        }

        public static DataTable dtRoleName =new DataTable("RoleName");
        public static DataTable dtModl=new DataTable("Model");

        public static void RegRole()
        {
            dtRoleName.Columns.Add("ID", Type.GetType("System.Int32"));
            dtRoleName.Columns.Add("Name", Type.GetType("System.String"));
            dtRoleName.Rows.Add(0, "系统管理员");
            dtRoleName.Rows.Add(1, "销售经理");
            dtRoleName.Rows.Add(2, "销售员");
            dtRoleName.Rows.Add(3, "仓库管理员");
        }


        /// <summary>
        /// 系统模块注册
        /// </summary>
        public static void RegModel()
        {
            dtModl.Columns.Add("ID", Type.GetType("System.Int32"));
            dtModl.Columns.Add("Name", Type.GetType("System.String"));
            dtModl.Rows.Add(0, "系统管理");
            dtModl.Rows.Add(1, "产品管理");
            dtModl.Rows.Add(2, "客户管理");
            dtModl.Rows.Add(3, "销售管理");
            dtModl.Rows.Add(4, "报表查看");
        }


        #region 系统模块枚举
        ///// <summary>
        ///// 系统模块枚举
        ///// </summary>
        public  enum JYModels
        {
            /// <summary>
            /// 系统管理
            /// </summary>
            系统管理 = 0,
            /// <summary>
            /// 产品管理
            /// </summary>
            产品管理 = 1,
            /// <summary>
            /// 客户管理
            /// </summary>
            客户管理 = 2,
            /// <summary>
            /// 销售管理
            /// </summary>
            销售管理= 3,
            /// <summary>
            /// 报表
            /// </summary>
            报表查看= 4
        }
        #endregion

        #region 权限类型枚举
        /// <summary>
        /// 权限类型枚举
        /// </summary>
        public enum RightType
        {
            /// <summary>
            /// 添加
            /// </summary>
            Add = 0,
            /// <summary>
            /// 删除
            /// </summary>
            Delete = 1,
            /// <summary>
            /// 修改
            /// </summary>
            Edit = 2,
            /// <summary>
            /// 保存
            /// </summary>
            Save=3,
            /// <summary>
            /// 查询
            /// </summary>
            Query = 4,
            /// <summary>
            /// 提交
            /// </summary>
            Commit=5,
            /// <summary>
            /// 审批
            /// </summary>
            Approval=6,
            /// <summary>
            /// 驳回
            /// </summary>
            Reject=7
        }
        #endregion
    }
}
